﻿using DLL.NET.BindingInjector;
using DLL.NET70.Infrastructure.Prism.MVVM;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Module.BindingInjector.ViewModels
{
    public class PUC_GetInjectorBarViewModel : CPDMVVMBase
    {
        private string _scanBar = "";

        public string ScanBar
        {
            get { return _scanBar; }
            set { SetProperty(ref _scanBar, value); }
        }

        private bool _barFocus = false;

        public bool BarFocus
        {
            get { return _barFocus; }
            set
            {
                SetProperty(ref _barFocus, value);
            }
        }

        public PUC_GetInjectorBarViewModel() : base()
        {
        }

        public override void Loaded()
        {
            BarFocus = true;
            base.Loaded();
        }

        private int UseCount = 0;

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            //base.OnDialogOpened(parameters);
            UseCount = parameters.GetValue<int>("Value");
        }

        #region interface delegate command

        public DelegateCommand<object> KeyDownCommand
        {
            get { return new DelegateCommand<object>(KeyDown); }
        }

        public DelegateCommand<object> KeyUpCommand
        {
            get { return new DelegateCommand<object>(KeyUp); }
        }

        //EnterKeyCommand
        //public DelegateCommand EnterKeyCommand
        //{
        //    get { return new DelegateCommand(EnterKey); }
        //}

        //LostFocusCommand
        public DelegateCommand LostFocusCommand
        {
            get { return new DelegateCommand(LostFocus); }
        }

        public DelegateCommand GetFocusCommand
        {
            get { return new DelegateCommand(GetFocus); }
        }

        //按键按下响应函数
        private void KeyDown(object sender)
        {
            //TextBox text = sender as TextBox;
            //if (text != null)
            //{
            //    string str = text.Text;
            //}
        }

        //按键弹起响应函数
        private void KeyUp(object sender)
        {
            //TextBox text = sender as TextBox;
            //if (text != null)
            //{
            //    string str = text.Text;
            //}
            if (ScanBar.Length >= 40)
            {
                EnterKey();
            }
        }

        //响应回车键

        private void LostFocus()
        {
            //RosyBrown
            //BarFocus = false;
            //TextBackGround = "Gray";
        }

        private void GetFocus()
        {
            //TextBackGround = "Green";
        }

        #endregion interface delegate command

        //EnterKeyCommand
        public DelegateCommand EnterKeyCommand
        {
            get { return new DelegateCommand(EnterKey); }
        }

        private int count = 0;

        private void EnterKey()
        {
            try
            {
                string[] values = ScanBar.Split("_");
                int index = values.Length - 1;
                bool re = CYanZheng.YanzhengShouquanma(values[index]);
                if (true == re)
                {
                    count++;
                    if ((count == UseCount))
                    {
                        CloseDialog();
                    }
                    Message = "继续扫描注射器授权码！";
                }
                else
                {
                    Message = "授权码无效！";
                }
            }
            catch (Exception e)
            {
                Message = "数据验证出现异常:" + e.Message;
            }

            ScanBar = "";
            //设置焦点
            BarFocus = true;
        }

        public DelegateCommand CloseCommand
        {
            get { return new DelegateCommand(Close); }
        }

        private void Close()
        {
            ButtonResult result = ButtonResult.No;
            RaiseRequestClose(new DialogResult(result));
        }
    }
}